package com.liujun.microserver.auth2.webjoin.server.security;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.liujun.microserver.auth2.webjoin.server.bean.ClientUser;
import com.liujun.microserver.auth2.webjoin.server.dao.UserRepository;

/**
 * 实现用户信息加载的接口
 * 
 * @author liujun
 * @date 2018/07/07
 * @date 2018/07/07 23:35:52
 */
@Service
public class ClientUserDetailsService implements UserDetailsService {

	/**
	 * doc:用户信息查询
	 */
	@Autowired
	private UserRepository users;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		Optional<ClientUser> optionalUser = users.findByUsername(username);

		if (!optionalUser.isPresent()) {
			throw new UsernameNotFoundException("invalid username or password");
		}

		return new ClientUserDetails(optionalUser.get());

	}

}
